<?php
/*
Plugin Name: Posts Plus Widget
Plugin URI: http://tbicl.org/#
Description: A little help for my friends.
Author: D. Kretz
Version: 0.0.1
Author URI: http://localhost
*/

/*
    option values:  title:
                    category:
*/

class Widget_Posts_Plus extends WP_Widget 
{

	function Widget_Posts_Plus() 
    {
        $_widget_description = __("Selected posts");
        $_classname          = "posts_plus";
        $_id_base            = "postsplus";
        $_widget_name        = "Posts Plus";

        $_widget_options = array("description" => $_widget_description,
                                 "classname"   => $_classname);
        $_control_options = array();
		// $control_opts = array("description" => __( "Selected posts" ) );
		// $this->WP_Widget("posts_plus", __("Posts Plus"), $control_opts);

        $this->WP_Widget($_id_base,
                         $_widget_name, 
                         // option array passed to register_sidebar_widget
                         $_widget_options,  
                         // option array passed to register_widget_control
                         $_control_options);

	}

  /**
    * Displays the Widget
    *
    */
	function widget( $args, $instance ) 
    {
		// extract($args, EXTR_SKIP);

        // options are widget subtitle (named "title") and category to select
        $title = isset($instance["title"]) ? $instance["title"] : $instance["category"];
		$category = isset($instance["category"]) ? $instance["category"] : false;
		$category_name = isset($category) ? get_category($category)->name : false;

      # Before the widget
        echo $args["before_widget"];

      # Use the category if the title is missing
        if($title) {
            echo $args["before_title"] . $title . $args["after_title"];
        }
        else {
            echo $args["before_title"] . $category_name . $args["after_title"];
        }

        $ary = array('category'=>$category,
                         'order'=>'DESC',
                         'orderby'=>'date');
        $postslist = get_posts($ary);
        if(count($postslist) > 0) {
            $post = $postslist[0];
            $val = topara($post->post_content);
            // <h2>{$post->post_title}</h2></div>
            echo "{$val}";
        }
        else {
            echo "<p>Sorry, nothing posted yet.</p>\n";
        }
      # After the widget
      echo $args["after_widget"];
	}

  /**
    * Saves the widgets settings.
    *
    */
	function update( $new_instance, $old_instance ) 
    {
        $instance = $old_instance;
		$instance["title"] = strip_tags($new_instance["title"]);
		$instance["category"] = empty($new_instance["category"]) 
            ? false : $new_instance["category"];
		// $instance["disptitle"] = empty($new_instance["disptitle"]) 
            // ? false : $new_instance["disptitle"];
		return $instance;
	}

  /**
    * Creates the edit form for the widget.
    *
    */
	function form( $instance ) 
    {

		//Defaults
		$instance = wp_parse_args( (array) $instance, 
                                    array( "title"       => false,
                                           "category"    => false));
                                           // "disptitle"   => false) );

        // for the category pick list
		$categories     = get_categories(array("hide_empty" => 0,
                                               "orderby" => "name"));

        $titleid        = $this->get_field_id("title");
        $titlename      = $this->get_field_name("title");
        $titlecaption   = __("Title");
        $titleval       = empty($instance["title"]) 
                              ? "" 
                              : attribute_escape(stripslashes($instance["title"])); 

        $catid          = $this->get_field_id("category");
        $catname        = $this->get_field_name("category");
        $catval         = empty($instance["category"]) 
                            ? false 
                            : $instance["category"];

        echo   "<label for='{$titleid}'>{$titlecaption}</label>
                <input class='widefat' 
                       type='text' 
                       id='{$titlename}'
                       name='${titlename}' 
                value='{$titleval}' /><br />\n";

		echo "<p>
            <label for='{$catid}'>";
            _e('Category'); 
            echo "</label>
		<select class='widefat' 
            id='{$catname}' 
            name='{$catname}'>\n";

        foreach ( $categories as $cat ) {
            $id = $cat->term_id;
            $name = $cat->name;
            echo "<option value='{$id}'";
            if(intval($id) == intval($catval))
                echo " selected='selected'";
            echo ">{$name}</option>\n";
        }
		echo "
		</select>
        </p>\n";
        
	}

    private function _echo_check_box($optname, $prompt) 
    {
        echo "<label for='".$this->get_field_id($optname)."'>".__($prompt)."</label><br />
        ";
        echo "<input type='checkbox' class='checkbox' ";
        checked($this->instance[$optname], true);
        echo " id='".$this->get_field_id($optname)."' ";
        echo " name='".$this->get_field_name($optname)."' />
        ";
    }
} // END class

/**
  * Register widget.
  *
  * Calls 'widgets_init' action after the widget has been registered.
  */
function widget_posts_plus_init() 
{
	register_widget("Widget_Posts_Plus");
}

add_action("init", "widget_posts_plus_init", 1);
?>
